<?php
protectScript('TodoListModel.php');

class TodoListModel{
	private static $_asmReadOnlyProps = array(
		'sTableName' => 'task',
		'sTablePrefix' => TABLE_PREFIX
	);

	private $_asmWritableProps = array(
		'iIdTask' => 0,
		'iDateBeginTask' => 0,
		'iDateEndTask' => 0,
		'sLabelTask' => '',
		'sDescriptionTask' => '',
		'iStatusTask' => 1,
		'iPriorityTask' => 0
	);

	const TODO = 1;
	const DONE = 2;
	const DOIN = 3;

	public function __construct($asmTodoListInfo = null){
		$this->_asmWritableProps['iIdTask'] = (isset($asmTodoListInfo['idTask'])) ? $asmTodoListInfo['idTask'] : $this->_asmWritableProps['iIdTask'];
		$this->_asmWritableProps['iDateBeginTask'] = (isset($asmTodoListInfo['dateBeginTask'])) ? $asmTodoListInfo['dateBeginTask'] : $this->_asmWritableProps['iDateBeginTask'];
		$this->_asmWritableProps['iDateEndTask'] = (isset($asmTodoListInfo['dateEndTask'])) ? $asmTodoListInfo['dateEndTask'] : $this->_asmWritableProps['iDateEndTask'];
		$this->_asmWritableProps['sLabelTask'] = (isset($asmTodoListInfo['labelTask'])) ? $asmTodoListInfo['labelTask'] : $this->_asmWritableProps['sLabelTask'];
		$this->_asmWritableProps['sDescriptionTask'] = (isset($asmTodoListInfo['descriptionTask'])) ? $asmTodoListInfo['descriptionTask'] : $this->_asmWritableProps['sDescriptionTask'];
		$this->_asmWritableProps['iStatusTask'] = (isset($asmTodoListInfo['statusTask'])) ? $asmTodoListInfo['statusTask'] : $this->_asmWritableProps['iStatusTask'];
		$this->_asmWritableProps['iPriorityTask'] = (isset($asmTodoListInfo['priorityTask'])) ? $asmTodoListInfo['priorityTask'] : $this->_asmWritableProps['iPriorityTask'];
	}

	public function getTasks($sWhereClause = null){
		$oMysqliInstance = AYSMysqli::getInstance();

		if(isset($sWhereClause)){
			$sWhere = "$sWhereClause";
		}else{
			$sWhere = '';
		}
		$query = sprintf(
			'SELECT idTask, dateBeginTask, dateEndTask, labelTask, descriptionTask, statusTask, priorityTask ' .
			"FROM %s " . $sWhere . " ORDER BY priorityTask",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName']
		);
		$rQueryResult = $oMysqliInstance->query($query);

		$aiasmTasksList = array();
		while($r = $rQueryResult->fetch_assoc()){
			$aiasmTasksList[] = $r;
		}
		return $aiasmTasksList;
	}

	public function updateStatus($iIdTask, $iTaskStatus){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"UPDATE %s " .
			"SET statusTask = %d" .
			"WHERE idTask = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$iTaskStatus,
			$iIdTask
		);
		$oMysqliInstance->query($query);
	}

	public function updatePriority($iIdTask, $iTaskPriority){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"UPDATE %s " .
			"SET priorityTask = %d" .
			"WHERE idTask = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$iTaskPriority,
			$iIdTask
		);
		$oMysqliInstance->query($query);
	}

	public function updateDate($iIdTask, $sTypeDate){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"UPDATE %s " .
			"SET %s = NOW()" .
			"WHERE idTask = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$sTypeDate,
			$iIdTask
		);
		$oMysqliInstance->query($query);
	}

	public static function addTask($assTaskInfos){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"INSERT INTO %s (labelTask, descriptionTask, statusTask, priorityTask)".
			"VALUES ('%s', '%s', %d, %d)",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$oMysqliInstance->escape_string($assTaskInfos['labelTask']),
			$oMysqliInstance->escape_string($assTaskInfos['descriptionTask']),
			$assTaskInfos['statusTask'],
			$assTaskInfos['priorityTask']
		);
		$oMysqliInstance->query($query);
	}

	public function editTask($assTaskInfos){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"UPDATE %s ".
			"SET labelTask = '%s', descriptionTask = '%s', statusTask = %d, priorityTask = %d ".
			"WHERE idTask = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$oMysqliInstance->escape_string($assTaskInfos['labelTask']),
			$oMysqliInstance->escape_string($assTaskInfos['descriptionTask']),
			$assTaskInfos['statusTask'],
			$assTaskInfos['priorityTask'],
			$assTaskInfos['idTask']
		);

		$oMysqliInstance->query($query);
	}

	public static function delTask($iIdTask){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"DELETE FROM %s ".
			"WHERE idTask = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$iIdTask
		);
		$oMysqliInstance->query($query);
	}

	public function __get($sVariableName){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			if(is_array($this->_asmReadOnlyProps[$sVariableName])){
				return clone new ArrayObject($this->_asmReadOnlyProps[$sVariableName]);
			}elseif(is_object($this->_asmReadOnlyProps[$sVariableName])){
				return clone $this->_asmReadOnlyProps[$sVariableName];
			}else{
				return $this->_asmReadOnlyProps[$sVariableName];
			}
		}elseif(!isset($this->_asmWritableProps[$sVariableName])){
			$this->_asmWritableProps[$sVariableName] = NULL;
		}

		return $this->_asmWritableProps[$sVariableName];
	}

	public function __set($sVariableName, $mValueVariable){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			throw new SetReadOnlyPropsException(
				'Tried to set a read only property on the event object',
				ERROR,
				array(
					'scriptName' => 'TodoListModel.php',
					'variableName' => $sVariableName
				)
			);
		}

		return $this->_asmWritableProps[$sVariableName] = $mValueVariable;
	}
}
